package com.ustadzmenjawab.server;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SearcherWarmer;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.util.Version;

import com.ustadzmenjawab.indexing.UstadzMenjawabSynAnalyzer;
import com.ustadzmenjawab.shared.Constant;

public class CustomSearchWarmer implements SearcherWarmer {

	@Override
	public void warm(IndexSearcher searcher) throws IOException {
		String[] fields  = {"judul","artikel","pertanyaan","offline"};			
		
		BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
				BooleanClause.Occur.SHOULD,
				BooleanClause.Occur.SHOULD,
				BooleanClause.Occur.MUST
		};
		Analyzer a = new UstadzMenjawabSynAnalyzer(Version.LUCENE_35);
		try {
			String [] initSearches = {"shalat"};
			for(String querystr: initSearches){
				String [] finalQuery = new String[]{querystr,querystr,querystr,"N"};
				Query q = MultiFieldQueryParser.parse(Version.LUCENE_35, finalQuery, fields,flags, a);
				TopScoreDocCollector collector = TopScoreDocCollector.create(Constant.HIT_PER_PAGE, true);
				searcher.search(q, collector);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
